Skip to content

fix(web): filter system-injected XML tags from rendering as raw text#387

Merged
tiann merged 6 commits intotiann:mainfrom
hqhq1025:fix/web-filter-system-injected-messages
Apr 2, 2026
Merged

fix(web): filter system-injected XML tags from rendering as raw text#387
tiann merged 6 commits intotiann:mainfrom
hqhq1025:fix/web-filter-system-injected-messages

Conversation

@hqhq1025
Copy link
Copy Markdown
Contributor

@hqhq1025 hqhq1025 commented Apr 1, 2026

Summary

  • Claude Code injects internal messages (<task-notification>, <system-reminder>, <command-name>, <local-command-caveat>) as user-role messages for model context. The web UI was rendering these as raw XML text visible to users.
  • <task-notification> is now parsed and displayed as an agent-event block showing the summary text (e.g., "Background command X was stopped")
  • <system-reminder>, <command-name>, and <local-command-caveat> are silently dropped
  • Mirrors the SYSTEM_INJECTION_PREFIXES list from cli/src/api/apiSession.ts to keep filtering in sync

Test plan

  • Added reducerTimeline.test.ts with 8 tests covering:
    • <task-notification> with summary → agent-event
    • <task-notification> without summary → silently dropped
    • <task-notification> with empty <summary></summary> → silently dropped
    • <task-notification> with leading whitespace → handled correctly
    • <system-reminder> → hidden
    • <command-name> → hidden
    • <local-command-caveat> → hidden
    • Normal user text → passes through as user-text block
  • bun run typecheck passes
  • All 68 tests pass

Copy link
Copy Markdown

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Findings

  • [Major] Generic XML filtering now rewrites or drops legitimate human prompts across non-Claude sessions, because reduceTimeline() applies the Claude-specific tag heuristic to every role: 'user' message. Codex and Gemini local sessions both forward human input through sendUserMessage() as plain user text (cli/src/api/apiSession.ts:407, cli/src/codex/session.ts:87, cli/src/gemini/session.ts:93), so a prompt that starts with <system-reminder> disappears entirely and <task-notification>...<summary>...</summary> is rendered as an agent event instead of the user’s text. Evidence: web/src/chat/reducerTimeline.ts:75.

Summary
Review mode: initial
One major regression found in web/src/chat/reducerTimeline.ts: Claude-specific XML filtering was added at the generic timeline layer, so it now affects normal user messages for other agent flavors as well.

Testing
Not run (automation; bun is unavailable in this runner, so the added Vitest file could not be executed here)

HAPI Bot

})
continue
}
if (isSystemInjectedMessage(msg.content.text)) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MAJOR] This runs in the generic msg.role === 'user' path, so it also rewrites or drops real human prompts for non-Claude sessions. sendUserMessage() still forwards Codex/Gemini user input as plain role: 'user' text (cli/src/api/apiSession.ts:407, cli/src/codex/session.ts:87, cli/src/gemini/session.ts:93), which means a prompt starting with <system-reminder> now vanishes and a prompt starting with <task-notification> becomes an agent-event.

Suggested fix:

// Keep Claude transcript cleanup in normalizeUserOutput(), which only runs for
// Claude JSONL `output` payloads, instead of filtering every user message here.
if (trimmed.startsWith('<task-notification>')) {
    const summary = trimmed.match(/<summary>([\s\S]*?)<\/summary>/)?.[1]?.trim()
    if (!summary) return null
    return {
        id: messageId,
        localId,
        createdAt,
        role: 'event',
        content: { type: 'message', message: summary },
        isSidechain: false,
        meta
    }
}
if (
    trimmed.startsWith('<system-reminder>') ||
    trimmed.startsWith('<command-name>') ||
    trimmed.startsWith('<local-command-caveat>')
) {
    return null
}

Copy link
Copy Markdown

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Findings

  • [Major] <task-notification> conversion is still unreachable on the real Claude path, because injected XML is stored as role: 'agent' / type: 'output' in sendClaudeSessionMessage() and normalizeUserOutput() still returns null for <task-notification> before reduceTimeline() sees a msg.role === 'user'. The new reducer branch therefore only handles synthetic inputs like the new test, so the UI will keep silently dropping task notifications instead of rendering the promised agent event. Evidence web/src/chat/reducerTimeline.ts:77, related context web/src/chat/normalizeAgent.ts:117, cli/src/api/apiSession.ts:364, web/src/chat/reducerTimeline.test.ts:29.
    Suggested fix:
    if (typeof messageContent === 'string') {
        const trimmed = messageContent.trimStart()
        if (trimmed.startsWith('<task-notification>')) {
            const summary = trimmed.match(/<summary>([\s\S]*?)<\/summary>/)?.[1]?.trim()
            if (!summary) return null
            return {
                id: messageId,
                localId,
                createdAt,
                role: 'event',
                content: { type: 'message', message: summary },
                isSidechain: false,
                meta
            }
        }
        if (
            trimmed.startsWith('<system-reminder>') ||
            trimmed.startsWith('<command-name>') ||
            trimmed.startsWith('<local-command-caveat>')
        ) {
            return null
        }
    }

Summary
Review mode: follow-up after new commits
One major issue remains in the follow-up diff: the new reducer logic does not run on the actual Claude transcript shape for <task-notification>, so the user-visible regression is only partially fixed.

Testing
Not run (automation; bun is not installed in this runner)

HAPI Bot


if (msg.role === 'user') {
if (context.isClaudeSession) {
const taskSummary = parseTaskNotificationSummary(msg.content.text)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MAJOR] parseTaskNotificationSummary() is wired into the msg.role === 'user' reducer path, but real Claude injections never reach this branch. sendClaudeSessionMessage() stores non-external Claude XML as role: 'agent' / type: 'output' (cli/src/api/apiSession.ts:364), and normalizeUserOutput() still returns null for <task-notification> before reduction (web/src/chat/normalizeAgent.ts:117). That means the new agent-event behavior is unreachable in production, while the added test only covers a synthetic traced role: 'user' message (web/src/chat/reducerTimeline.test.ts:29).

Suggested fix:

if (typeof messageContent === 'string') {
    const trimmed = messageContent.trimStart()
    if (trimmed.startsWith('<task-notification>')) {
        const summary = trimmed.match(/<summary>([\s\S]*?)<\/summary>/)?.[1]?.trim()
        if (!summary) return null
        return {
            id: messageId,
            localId,
            createdAt,
            role: 'event',
            content: { type: 'message', message: summary },
            isSidechain: false,
            meta
        }
    }
    if (
        trimmed.startsWith('<system-reminder>') ||
        trimmed.startsWith('<command-name>') ||
        trimmed.startsWith('<local-command-caveat>')
    ) {
        return null
    }
}

@hqhq1025
Copy link
Copy Markdown
Contributor Author

hqhq1025 commented Apr 1, 2026

Thanks for the follow-up review!

Regarding the second finding — I've traced the full message flow and believe the current implementation is correct:

  1. CLI side: sendClaudeSessionMessage() passes the raw JSONL body as { role: 'agent', content: { type: 'output', data: body } } (line 378-388 in apiSession.ts)
  2. Web normalize: normalizeAgentRecord() sees data.type === 'user' → calls normalizeUserOutput() (line 208-209 in normalizeAgent.ts)
  3. normalizeUserOutput(): data.message is { content: "<task-notification>..." } which is a valid object, and message.content is a string → returns role: 'user' NormalizedMessage (line 123-132)
  4. reduceTimeline(): sees msg.role === 'user' → our filtering logic runs correctly

The comment in isExternalUserMessage() (line 55 in apiSession.ts) explicitly states: "All metadata fields (userType, isMeta, …) are identical to genuine user messages" — so these system injections are NOT filtered by isMeta at line 201. They do make it through normalizeUserOutput() as role: 'user' messages.

The normalizeUserOutput() function does NOT return null for <task-notification> — it returns a valid user message with the XML text as content, which then reaches our filtering code in reduceTimeline().

Copy link
Copy Markdown

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review mode: follow-up after new commits

Findings

  • [Major] The new <task-notification> handling in normalizeUserOutput() is still unreachable. web/src/chat/normalizeAgent.ts:114 returns null for <task-notification> and <system-reminder> before the added conversion/sidechain branch at web/src/chat/normalizeAgent.ts:137 runs, so the new expectations in web/src/chat/normalize.test.ts:108 and web/src/chat/normalize.test.ts:178 cannot pass. In production this still drops Claude task notifications instead of rendering the promised event.
  • [Minor] The new Claude-only fallback in web/src/chat/reducerTimeline.ts:75 runs on every normalized role: 'user' message, including genuine user input from web/src/chat/normalizeUser.ts:37 and optimistic sends in web/src/hooks/mutations/useSendMessage.ts:64. If a user pastes one of these XML tags into chat, their own message is silently dropped or rewritten.

Summary

  • 1 major issue remains in the follow-up patch, plus 1 narrower regression in the reducer fallback for real user messages.

Testing

  • Not run (automation; bun is unavailable in this runner)

HAPI Bot

isSidechain: false,
meta
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MAJOR] The new <task-notification> conversion here is still dead code because the old guard above it returns null for both <task-notification> and <system-reminder> first (web/src/chat/normalizeAgent.ts:114). That means the new expectations in web/src/chat/normalize.test.ts:108 / web/src/chat/normalize.test.ts:178 cannot pass, and Claude task notifications will still be dropped instead of rendered as role: 'event'.

Suggested fix:

if (typeof messageContent === 'string') {
    const trimmed = messageContent.trimStart()

    if (trimmed.startsWith('<task-notification>')) {
        const summary = trimmed.match(/<summary>([\s\S]*?)<\/summary>/)?.[1]?.trim()
        return summary ? {
            id: messageId,
            localId,
            createdAt,
            role: 'event',
            content: { type: 'message', message: summary },
            isSidechain: false,
            meta
        } : {
            id: messageId,
            localId,
            createdAt,
            role: 'agent',
            isSidechain: true,
            content: [{ type: 'sidechain', uuid, prompt: messageContent }]
        }
    }
}

}

if (msg.role === 'user') {
if (context.isClaudeSession) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MINOR] This new reducer branch runs on every normalized role: 'user' message in Claude sessions, not just injected XML. normalizeUserRecord() turns genuine human text into role: 'user' (web/src/chat/normalizeUser.ts:37), and optimistic sends use the same shape (web/src/hooks/mutations/useSendMessage.ts:64), so a user who pastes <task-notification>...</task-notification> or <system-reminder>...</system-reminder> into chat will lose or mutate their own message.

Suggested fix:

if (msg.role === 'user') {
    // Do not filter real user messages here.
    // Keep Claude XML handling in normalizeUserOutput(), where injected
    // type:'user' agent payloads are distinguishable from human input.
}

hqhq1025 added 4 commits April 2, 2026 11:35
Claude Code injects internal messages (<task-notification>, <system-reminder>,
<command-name>, <local-command-caveat>) as user-role messages. The web UI was
rendering these as raw XML text visible to users.

- Parse <task-notification> and display as agent-event with summary text
- Silently drop <system-reminder>, <command-name>, <local-command-caveat>
- Add tests covering all injection prefixes and edge cases
Address review feedback: the XML tag filtering was applied at the
generic timeline layer, which could incorrectly hide legitimate user
messages in Codex/Gemini sessions.

- Add isClaudeSession flag threaded from Session.metadata.claudeSessionId
- Only filter system-injected tags when isClaudeSession is true
- Add tests verifying non-Claude sessions pass through all messages
… leaks

Restores the fix from 3cf96ab that was accidentally reverted in 2205e04.

In normalizeUserOutput(), string-content user messages arriving through
the agent output path are never real user input (real user text goes
through normalizeUserRecord). Previously, non-sidechain string messages
were emitted as role:'user', causing subagent prompts and system-injected
messages to render as user text in the web UI.

Now all string-content user messages in this path are:
- <task-notification> with summary → converted to role:'event'
- Everything else → marked as sidechain (matched to parent Task tool
  call by the tracer, or harmlessly skipped by the reducer)

This provides a root-level fix that prevents ANY string user message
from the agent output path from leaking as visible user text.
Copy link
Copy Markdown

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review mode: follow-up after new commits

Findings

  • [Major] The new <task-notification> path is still unreachable because normalizeUserOutput() returns null for the same tag earlier, so real Claude task notifications never reach the reducer fallback. Evidence web/src/chat/normalizeAgent.ts:114, web/src/chat/normalizeAgent.ts:140, web/src/chat/reducerTimeline.ts:75.
  • [Minor] The new Claude-only reducer filter now runs on every normalized human role: 'user' message, so a user who pastes one of these XML tags will have their own message dropped or rewritten. Evidence web/src/chat/reducerTimeline.ts:75, web/src/chat/normalizeUser.ts:37, web/src/hooks/mutations/useSendMessage.ts:64.

Summary

  • Review mode: follow-up after new commits
  • 1 major issue remains, plus 1 minor regression in the new reducer fallback.

Testing

  • Not run (automation)

HAPI Bot

isSidechain: false,
meta
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MAJOR] This branch is still dead on the current head. normalizeUserOutput() returns null for <task-notification> a few lines earlier at web/src/chat/normalizeAgent.ts:114, so real Claude injections never reach this conversion and the new reducer fallback only affects synthetic role: 'user' messages from tests.

Suggested fix:

if (typeof messageContent === 'string') {
    const trimmed = messageContent.trimStart()

    if (trimmed.startsWith('<task-notification>')) {
        const summary = trimmed.match(/<summary>([\s\S]*?)<\/summary>/)?.[1]?.trim()
        return summary
            ? {
                id: messageId,
                localId,
                createdAt,
                role: 'event',
                content: { type: 'message', message: summary },
                isSidechain: false,
                meta
            }
            : {
                id: messageId,
                localId,
                createdAt,
                role: 'agent',
                isSidechain: true,
                content: [{ type: 'sidechain', uuid, prompt: messageContent }]
            }
    }
}

}

if (msg.role === 'user') {
if (context.isClaudeSession) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MINOR] This new branch runs on every normalized role: 'user' message in Claude sessions, including real human input from normalizeUserRecord() and optimistic sends. If a user pastes <task-notification>...</task-notification> or <system-reminder>...</system-reminder>, their own message is silently dropped or rewritten.

Suggested fix:

if (msg.role === 'user') {
    if (isCliOutputText(msg.content.text, msg.meta)) {
        // existing CLI-output handling
    }

    blocks.push({
        kind: 'user-text',
        id: msg.id,
        localId: msg.localId,
        createdAt: msg.createdAt,
        text: msg.content.text,
        attachments: msg.content.attachments,
        status: msg.status,
        originalText: msg.originalText,
        meta: msg.meta
    })
    continue
}

…#372

The upstream `return null` filter for <task-notification> and
<system-reminder> (from PR tiann#372) is now superseded by the comprehensive
sidechain upgrade logic. Remove it to avoid short-circuiting the new
task-notification → event conversion.
@hqhq1025 hqhq1025 force-pushed the fix/web-filter-system-injected-messages branch from 488dd6d to 5101e90 Compare April 2, 2026 03:36
Copy link
Copy Markdown

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Findings

  • [Minor] Reducer-side Claude XML filtering still runs on genuine user messages, so a user who pastes one of these tags will have their own text hidden or rewritten. Agent-path injections are already normalized earlier in normalizeUserOutput(), while persisted and optimistic human messages still come through normalizeUserRecord() as role: 'user'. Evidence web/src/chat/reducerTimeline.ts:75, web/src/chat/normalizeAgent.ts:123, web/src/chat/normalize.ts:23, web/src/chat/normalizeUser.ts:37.
    Suggested fix:
    if (msg.role === 'user') {
        if (isCliOutputText(msg.content.text, msg.meta)) {
            // existing CLI-output handling
        }
    
        blocks.push({
            kind: 'user-text',
            id: msg.id,
            localId: msg.localId,
            createdAt: msg.createdAt,
            text: msg.content.text,
            attachments: msg.content.attachments,
            status: msg.status,
            originalText: msg.originalText,
            meta: msg.meta
        })
        continue
    }

Summary
Review mode: follow-up after new commits

  • 1 minor regression remains in the new reducer fallback; the earlier unreachable-branch issue is fixed on this head.

Testing

  • Not run (automation; bun not available in runner)

HAPI Bot

}

if (msg.role === 'user') {
if (context.isClaudeSession) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MINOR] This Claude-only reducer fallback still runs on every normalized role: user message. Real human input from normalizeUserRecord() and optimistic sends already lands on this path, while injected type: "user" agent output is already handled earlier in normalizeUserOutput(). A Claude user who pastes <system-reminder>...</system-reminder> or <task-notification>...</task-notification> will have their own message dropped or rewritten.

Suggested fix:

if (msg.role === user) {
    if (isCliOutputText(msg.content.text, msg.meta)) {
        // existing CLI-output handling
    }

    blocks.push({
        kind: user-text,
        id: msg.id,
        localId: msg.localId,
        createdAt: msg.createdAt,
        text: msg.content.text,
        attachments: msg.content.attachments,
        status: msg.status,
        originalText: msg.originalText,
        meta: msg.meta
    })
    continue
}

System-injected messages are now fully handled in normalizeUserOutput()
(normalize layer), so the redundant filtering in reduceTimeline() is no
longer needed. Removing it also eliminates the risk of accidentally
hiding legitimate user messages that happen to start with XML tags.

- Remove SYSTEM_INJECTION_PREFIXES, isSystemInjectedMessage,
  parseTaskNotificationSummary from reducerTimeline.ts
- Remove isClaudeSession plumbing from reducer.ts and SessionChat.tsx
- Simplify reducerTimeline.test.ts to only test pass-through behavior
Copy link
Copy Markdown

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Findings

  • No issues found in added/modified lines.

Summary
Review mode: follow-up after new commits

  • No new correctness, security, or regression issues found in the latest diff.
  • Residual risk: the normalize-layer filtering added here is only directly covered for <task-notification> and <system-reminder> in the updated tests; <command-name>, <local-command-caveat>, leading whitespace, and empty-summary cases are still untested on this head.

Testing

  • Not run (automation; bun not available in runner)

HAPI Bot

@tiann tiann merged commit 36022a0 into tiann:main Apr 2, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants